package com.pickgliss.loaders
{
	import com.pickgliss.ds.BitmapLib;
	import com.pickgliss.events.LoaderEvent;
	import com.pickgliss.utils.DisplayUtils;
	
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.ByteArray;

	public class PngxLoader extends BitmapLoader
	{
		public function PngxLoader(url:String, callback:Function, catchable:Boolean=true)
		{
			super(url, callback, catchable);
		}

		/**
		 * 原始的 contentLoaderInfo.bytes 数据是经过flashplayer加工的 去除前面 54字节和结束的17字节就是原始数据
		 * @param fileIn
		 *
		 */
		override protected function __onContentLoadComplete(event:Event):void
		{
			var fileIn:ByteArray=DisplayUtils.readFromPngx(_displayLoader.contentLoaderInfo.bytes, true);
			if (fileIn != null)
			{
				fileIn.position=0;
				var frameCount:int=fileIn.readUnsignedInt();
				var tempFrames:Array=[];
				for (var i:int=0; i < frameCount; i++)
				{
					var frame:Object={};
					frame.centerPoint=new Point();
					frame.centerPoint.x=fileIn.readInt();
					frame.centerPoint.y=fileIn.readInt();
					frame.frameRect=new Rectangle();
					frame.frameRect.x=fileIn.readInt();
					frame.frameRect.y=fileIn.readInt();
					frame.frameRect.width=fileIn.readInt();
					frame.frameRect.height=fileIn.readInt();
					tempFrames.push(frame);
				}
				var actionCount:int=fileIn.readUnsignedInt();

				for (var j:int=0; j < actionCount; j++)
				{
					var actioninfo:ActionInfo=new ActionInfo();
					actioninfo.name=fileIn.readUTF();
					actioninfo.type=fileIn.readInt();
					var count:int=fileIn.readInt();
					for (var k:int=0; k < count; k++)
					{
						var index:int=fileIn.readInt();
						actioninfo.frameRects.push(tempFrames[index].frameRect);
						actioninfo.centerPos.push(tempFrames[index].centerPoint);
					}
					actions.push(actioninfo);
				}
			}
			super.__onContentLoadComplete(event);
		}
		
		public var actions:Vector.<ActionInfo> = new Vector.<ActionInfo>();

		public static function isPngxURL(url:String):Boolean
		{
			return false;
		}
	}
}
